package date0914;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

//4:根据主键删除subject，由用户输入要删除的主键
public class dele {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String url = "jdbc:mysql://localhost:3306/wrok";
		Connection con = null;
		Statement st = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			con = DriverManager.getConnection(url,"xjl","xjlroot");
			st = con.createStatement();
			System.out.println("输入你要删除的科目id");
			String id = input.next();
			int rs = st.executeUpdate("delete from subject where id =" + id + ";");
			if(rs > 0) System.out.println("删除成功！");
			else System.out.println("删除失败了！");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if(st != null) {
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
